package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Customer;
import com.bjpowernode.crm.workbench.domain.FunnelVO;
import com.bjpowernode.crm.workbench.domain.Tran;
import com.bjpowernode.crm.workbench.domain.TranHistory;
import com.bjpowernode.crm.workbench.dto.TranAddingDto;
import com.bjpowernode.crm.workbench.mapper.ActivityMapper;
import com.bjpowernode.crm.workbench.mapper.TranMapper;
import com.bjpowernode.crm.workbench.service.*;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;
import java.util.List;

@Getter
@Setter
public class TranServiceImpl implements TranService {

    /**
     * 访问交易数据
     */
    private TranMapper tranMapper;

    /**
     * 处理交易历史相关业务
     */
    private TranHistoryService tranHistoryService;

    /**
     * 处理客户相关业务
     */
    private CustomerService customerService;

    /**
     * 处理用户相关业务
     */
    private UserService userService;

    /**
     * 处理字典值相关业务
     */
    private DicValueService dicValueService;

    /**
     * 处理市场活动相关业务
     */
    private ActivityService activityService;

    /**
     * 处理联系人相关业务
     */
    private ContactsService contactsService;

    @Override
    public void addTran(Tran tran) {
        this.tranMapper.insertTran(tran);
    }

    @Override
    public void addTran(TranAddingDto tranAddingDto, User user) {
        String formattedDate = DateUtils.formatDateTime(new Date());
        //如果对应客户不存在，则创建
        String idOfCustomer = this.customerService.selectIdByName(tranAddingDto.getCustomerName());

        if (idOfCustomer == null || idOfCustomer.length() == 0) {//对应客户不存在
            //模型转换，新增
            idOfCustomer = UUIDUtils.getUUID();
            Customer customer = new Customer();

            customer.setOwner(user.getId());
            customer.setName(tranAddingDto.getCustomerName());
            customer.setId(idOfCustomer);
            customer.setCreateTime(formattedDate);
            customer.setCreateBy(user.getId());
            customer.setEditTime(formattedDate);
            customer.setEditBy(user.getId());

            this.customerService.addCustomer(customer);
        }

        //新增交易
        Tran tran = new Tran();
        tran.setStage(tranAddingDto.getStage());
        tran.setOwner(tranAddingDto.getOwner());
        tran.setNextContactTime(tranAddingDto.getNextContactTime());
        tran.setName(tranAddingDto.getName());
        tran.setMoney(tranAddingDto.getMoney());
        tran.setId(UUIDUtils.getUUID());
        tran.setExpectedDate(tranAddingDto.getExpectedDate());
        tran.setCustomerId(idOfCustomer);
        tran.setCreateTime(formattedDate);
        tran.setCreateBy(user.getId());
        tran.setEditTime(formattedDate);
        tran.setEditBy(user.getId());
        tran.setContactSummary(tranAddingDto.getContactSummary());
        tran.setContactsId(tranAddingDto.getContactsId());
        tran.setActivityId(tranAddingDto.getActivityId());
        tran.setDescription(tranAddingDto.getDescription());
        tran.setSource(tranAddingDto.getSource());
        tran.setType(tranAddingDto.getType());

        this.tranMapper.insertTran(tran);

        //新增交易历史
        TranHistory tranHistory = new TranHistory();
        tranHistory.setCreateBy(user.getId());
        tranHistory.setCreateTime(formattedDate);
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setId(UUIDUtils.getUUID());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setStage(tran.getStage());
        tranHistory.setTranId(tran.getId());

        this.tranHistoryService.addTranHistory(tranHistory);
    }

    @Override
    public Tran queryTranById(String id) {
        Tran tran = this.tranMapper.selectTranById(id);

        //数据库中存放的是所有者、创建者、最后一个编辑者的 id ，页面需要的是所有者的名称
        tran.setOwner(this.userService.queryNameById(tran.getOwner()));
        tran.setCreateBy(this.userService.queryNameById(tran.getCreateBy()));
        tran.setEditBy(this.userService.queryNameById(tran.getEditBy()));

        //数据库中存放的是客户的 id ，页面需要的是客户的名称
        //应该额外加一个表示客户名称的属性的
        tran.setCustomerId(this.customerService.queryNameById(tran.getCustomerId()));

        //数据库中存放的是阶段、类型、来源的字典 id，页面需要的是它们具体的值
        tran.setStage(this.dicValueService.queryValueById(tran.getStage()));
        tran.setType(this.dicValueService.queryValueById(tran.getType()));
        tran.setSource(this.dicValueService.queryValueById(tran.getSource()));

        //数据库中不包含阶段的顺序编号，但是页面需要
        tran.setOrderNo(this.dicValueService.queryOrderNoById(tran.getStage()));

        //数据库中存放的是对应市场活动的 id ，页面需要的是它的名称
        //这里应该额外加个属性表示市场活动的名称的
        tran.setActivityId(this.activityService.queryNameById(tran.getActivityId()));

        //数据库中存放的是联系人的 id ，页面需要的是联系人的全名
//        这里应该额外加个属性表示联系人的全名的
        tran.setContactsId(this.contactsService.queryFullnameById(tran.getContactsId()));

        return tran;
    }

    @Override
    public List<FunnelVO> queryCountOfEveryStage() {
        return this.tranMapper.selectCountOfEveryStage();
    }

}
